package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){
        JSONObject jsonObject = (JSONObject) map.get("Role");
        JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");

        Role role = jsonObject.toJavaObject(Role.class);
        Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});
        Integer[] menuIds = jsonArray2.toArray(new Integer[]{});

        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        JSONObject jsonObject = (JSONObject) map.get("Role");
        JSONArray jsonArray1 = (JSONArray) map.get("permissionIds");
        JSONArray jsonArray2 = (JSONArray) map.get("menuIds");

        Role role = jsonObject.toJavaObject(Role.class);
        Integer[] permissionIds = jsonArray1.toArray(new Integer[]{});
        Integer[] menuIds = jsonArray2.toArray(new Integer[]{});

        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer roleId){
        try {
            roleService.delById(roleId);
            return new Result(true,MessageConst.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPermissions")
    public Result findPermissions(Integer roleId){
        try {
            Integer[] permissions = roleService.findPermissions(roleId);
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/findMenus")
    public Result findMenus(Integer roleId){
        try {
            Integer[] menus = roleService.findMenus(roleId);
            return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }
    }
}
